package com.jms.web.controller.jms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.jms.common.core.domain.entity.SysDept;
import com.jms.common.filter.utils.SecurityUtils;
import com.jms.common.filter.utils.poi.ExcelUtil;
import com.jms.system.service.ISysDeptService;
import com.jms.web.controller.jms.domain.Enterprise;
import com.jms.web.controller.jms.service.IEnterpriseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.jms.common.annotation.Log;
import com.jms.common.core.controller.BaseController;
import com.jms.common.core.domain.AjaxResult;
import com.jms.common.enums.BusinessType;
import com.jms.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 企业Controller
 * 
 * @author drx
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/system/enterprise")
public class EnterpriseController extends BaseController
{
    @Resource
    private IEnterpriseService enterpriseService;

    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 查询企业列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list(Enterprise enterprise)
    {
        /*
         * 获取当前用户部门区划编码
         * */
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        String areaShortCode = sysDept.getAreaShortCode();

        areaShortCode = areaShortCode.replaceAll("0+$", "").replaceAll("\\.$", "");

        enterprise.setAreaShortCode(areaShortCode);

        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 查询企业列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:list')")
    @PostMapping("/getList")
    public TableDataInfo getList(@RequestBody Enterprise enterprise)
    {

        /*
         * 获取当前用户部门区划编码
         * */
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        String areaShortCode = sysDept.getAreaShortCode();

        areaShortCode = areaShortCode.replaceAll("0+$", "").replaceAll("\\.$", "");

        enterprise.setAreaShortCode(areaShortCode);

        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 导出企业列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:export')")
    @Log(title = "企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Enterprise enterprise)
    {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        util.exportExcel(response, list, "企业数据");
    }

    /**
     * 获取企业详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(enterpriseService.selectEnterpriseById(id));
    }

    /**
     * 新增企业
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:add')")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Enterprise enterprise)
    {
        return toAjax(enterpriseService.insertEnterprise(enterprise));
    }

    /**
     * 修改企业
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:edit')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Enterprise enterprise)
    {
        return toAjax(enterpriseService.updateEnterprise(enterprise));
    }

    /**
     * 删除企业
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:remove')")
    @Log(title = "企业", businessType = BusinessType.DELETE)
	@GetMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(enterpriseService.deleteEnterpriseByIds(ids));
    }

    /*
    * 查询企业名称和id
    * */
    @GetMapping("/getEnterpriseDict")
    @ResponseBody
    public AjaxResult getEnterpriseDict(){
        return enterpriseService.getEnterpriseDict();
    }
}
